package com.gzhryc.net_marketing.cloud.micro.api.controllers.cloud;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.cloud.notice.SupplyChainAPIService;
import com.gzhryc.net_marketing.api.protocol.config.*;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccount;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountService;
import com.gzhryc.net_marketing.cloud.business.dao.db.MarketProjectApp;
import com.gzhryc.net_marketing.cloud.business.dao.db.MarketSupplier;
import com.gzhryc.net_marketing.cloud.business.dao.enums.MarketProjectAppEnum;
import com.gzhryc.net_marketing.cloud.business.dao.enums.MarketSupplierEnum;
import com.gzhryc.net_marketing.cloud.business.services.MarketProjectAppService;
import com.gzhryc.net_marketing.cloud.business.services.MarketSupplierService;
import com.gzhryc.net_marketing.cloud.business.services.dto.MarketProjectAppSearch;
import com.gzhryc.net_marketing.cloud.business.services.dto.MarketSupplierSearch;
import com.gzhryc.net_marketing.cloud.micro.api.MicroConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.config.dao.db.LogisticsCompany;
import com.gzhryc.shared_device.oem.code.config.services.LogisticsCompanyService;
import com.gzhryc.shared_device.oem.code.config.services.dto.LogisticsCompanySearch;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class ConfigAPIController {

    static Logger log = Logger.getLogger(ConfigAPIController.class);

    @Route(value = { "/market/cloud_api/v1/project/config.data" })
    public MarketProjectAppConfigResponse projectConfig(HttpServletRequest request) {
    	MarketProjectAppConfigResponse apiResp = new MarketProjectAppConfigResponse(DefaultAPICodeConstants.NULL_DATA,"暂无数据");
    	MarketProjectAppConfigRequest apiReq = WebUtils.getJsonRequest(request, MarketProjectAppConfigRequest.class);
        if(apiReq != null && StringTools.isNotBlank(apiReq.getBody().getToken())) {
            MarketProjectAppSearch search = new MarketProjectAppSearch();
            search.setState(MarketProjectAppEnum.State.Enable.index());
            List<MarketProjectApp> marketAppList = MarketProjectAppService.self().findBySearch(search);

            if(marketAppList != null && marketAppList.size() > 0){
                apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                apiResp.getBody().setItems(new ArrayList<>());
                for(MarketProjectApp marketApp : marketAppList){
                	MarketProjectAppConfigResponse.AppItem appItem = new MarketProjectAppConfigResponse.AppItem();
                    appItem.setId(marketApp.getId());
                    appItem.setName(marketApp.getName());
                    appItem.setDbConfig(Base64.getEncoder().encodeToString(marketApp.getDbConfig().getBytes(StandardCharsets.UTF_8)));
                    appItem.setCreateDate(marketApp.getCreateDate());

                    CloudPayAccount payAccount = CloudPayAccountService.self().getById(marketApp.getAccountId());
                    if(payAccount != null) {
                        appItem.setAccountId(payAccount.getId());
                        appItem.setPayToken(payAccount.getPayToken());
                        appItem.setVisitToken(payAccount.getVisitToken());
                    }
                    apiResp.getBody().getItems().add(appItem);
                }
            }
        }
        return apiResp;
    }
    
    @Route(value = { "/market/cloud_api/v1/supplier/config.data" })
    public MarketSupplierConfigResponse supplierConfig(HttpServletRequest request) {
    	MarketSupplierConfigResponse apiResp = new MarketSupplierConfigResponse(DefaultAPICodeConstants.NULL_DATA,"暂无数据");
    	MarketSupplierConfigRequest apiReq = WebUtils.getJsonRequest(request, MarketSupplierConfigRequest.class);
        if(apiReq != null && StringTools.isNotBlank(apiReq.getBody().getToken())) {
        	MarketSupplierSearch search = new MarketSupplierSearch();
        	search.setState(MarketSupplierEnum.State.Enable.index());
            List<MarketSupplier> supplierList = MarketSupplierService.self().findBySearch(search,1,MicroConstants.PAGE_LIMIT);

            if(supplierList != null && supplierList.size() > 0){
                apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                apiResp.getBody().setItems(new ArrayList<>());
                for(MarketSupplier supplier : supplierList){
                	MarketSupplierConfigResponse.SupplierItem supplierItem = new MarketSupplierConfigResponse.SupplierItem();
                	supplierItem.setId(supplier.getId());
                	supplierItem.setUserId(supplier.getUserId());
                	supplierItem.setName(supplier.getName());
                	supplierItem.setDbConfig(Base64.getEncoder().encodeToString(supplier.getDbConfig().getBytes(StandardCharsets.UTF_8)));
                	supplierItem.setCreateDate(supplier.getCreateDate());

                    CloudPayAccount payAccount = CloudPayAccountService.self().getById(supplier.getAccountId());
                    if(payAccount != null) {
                    	supplierItem.setAccountId(payAccount.getId());
                    	supplierItem.setPayToken(payAccount.getPayToken());
                    	supplierItem.setVisitToken(payAccount.getVisitToken());
                    }
                    apiResp.getBody().getItems().add(supplierItem);
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/market/cloud_api/v1/logistics/company/list.data" })
    public LogisticsCompanyListResponse findLogisticsCompany(HttpServletRequest request) {
        LogisticsCompanyListResponse apiResp = new LogisticsCompanyListResponse(DefaultAPICodeConstants.NULL_DATA,"暂无数据");
        LogisticsCompanyListRequest apiReq = WebUtils.getJsonRequest(request,LogisticsCompanyListRequest.class);
        if(apiReq != null) {
            Integer pageNo = apiReq.getBody().getPageNo() != null?apiReq.getBody().getPageNo():1;
            Integer pageLimit = apiReq.getBody().getPageLimit() != null?apiReq.getBody().getPageLimit():30;

            LogisticsCompanySearch search = new LogisticsCompanySearch();
            search.setName(apiReq.getBody().getName());

            apiResp.getBody().setTotal(LogisticsCompanyService.self().countBySearch(search));

            List<LogisticsCompany> logisticsCompanyList = LogisticsCompanyService.self(BaseConstants.JDBC_KEY).findBySearch(search,pageNo,pageLimit);
            if(logisticsCompanyList != null && logisticsCompanyList.size()  > 0){
                apiResp.setCode(DefaultAPICodeConstants.SUCCESS);
                apiResp.getBody().setItems(new ArrayList<>());
                for(LogisticsCompany company : logisticsCompanyList){
                    LogisticsCompanyListResponse.CompanyItem item = new LogisticsCompanyListResponse.CompanyItem();
                    item.setId(company.getId());
                    item.setName(company.getName());
                    apiResp.getBody().getItems().add(item);
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/market/cloud_api/v1/logistics/cost/list.data" })
    public LogisticsCostListResponse findLogisticsCost(HttpServletRequest request){
        LogisticsCostListResponse apiResp = new LogisticsCostListResponse(DefaultAPICodeConstants.NULL_DATA,"暂无数据");
        LogisticsCostListRequest apiReq = WebUtils.getJsonRequest(request, LogisticsCostListRequest.class);
        log.info("物流价格列表请求：{{0:json}}",apiReq);
        if(apiReq != null && apiReq.getBody().getSupplyId() != null) {
            LogisticsCostListResponse temp = SupplyChainAPIService.self().findLogisticsCost(apiReq.getBody().getSupplyId(),apiReq);
            if(temp != null){
                apiResp = temp;
            }
        }else{
            apiResp.setCode(DefaultAPICodeConstants.EXECUTE_ERROR,"参数错误");
        }
        return apiResp;
    }

    @Route(value = { "/market/cloud_api/v1/logistics/cost/usable.data" })
    public LogisticsUsableCostResponse getLogisticsUsableCost(HttpServletRequest request){
        LogisticsUsableCostResponse apiResp = new LogisticsUsableCostResponse(DefaultAPICodeConstants.NULL_DATA,"暂无数据");
        LogisticsUsableCostRequest apiReq = WebUtils.getJsonRequest(request, LogisticsUsableCostRequest.class);
        log.info("获取可用物流价格请求：{{0:json}}",apiReq);
        if(apiReq != null && apiReq.getBody().getSupplyId() != null) {
            LogisticsUsableCostResponse temp = SupplyChainAPIService.self().getLogisticsUsableCost(apiReq.getBody().getSupplyId(),apiReq);
            if(temp != null){
                apiResp = temp;
            }
        }else{
            apiResp.setCode(DefaultAPICodeConstants.EXECUTE_ERROR,"参数错误");
        }
        return apiResp;
    }
}
